<template>
  <div class="HmRegister">
    <titleNav>
      <template #default>
        <div class="title">面经注册</div>
      </template>
    </titleNav>
    <van-form @submit="onSubmit">
      <van-field v-model="username" name="用户名" label="用户名" placeholder="用户名" :rules="[{ required: true, message: '请填写用户名' }]" />
      <van-field v-model="password" type="password" name="密码" label="密码" placeholder="密码" :rules="[{ required: true, message: '请填写密码' }]" submit-on-enter autocomplete="off" />
      <div style="margin: 16px">
        <van-button round block type="info" native-type="submit" class="loginBtn" @click="onLogin">注册</van-button>
      </div>
    </van-form>
    <div class="enroll">
      <div class="us">
        <router-link to="/login">有账号,去登陆</router-link>
      </div>
    </div>
  </div>
</template>

<script>
import titleNav from '@/components/titleNav.vue'
import axios from 'axios'

// import getToken from '@/utils/storage'
export default {
  name: 'HmRegister',
  components: {
    titleNav,
  },
  data() {
    return {
      isInput: true,
      username: '',
      password: '',
    }
  },
  methods: {
    async onSubmit(values) {
      console.log('submit', values)
    },
    async onLogin() {
      const res = await axios({
        method: 'post',
        url: 'http://interview-api-t.itheima.net/h5/user/register',
        headers: {
          'Content-Type': 'application/json',
        },
        data: {
          username: this.username,
          password: this.password,
        },
      })
      if (res.status === 200) {
        alert('注册成功')
        this.$router.push('/login')
      }
      console.log(res)
    },
  },
}
</script>

<style lang="less" scoped>
.loginBtn {
  border: none;
  border-radius: 0;
  background-color: #07c160;
}
.enroll {
  display: flex;
  justify-content: end;
  padding-right: 20px;
}
.enroll .us a {
  color: #1c7ab0;
  font-size: 14px;
}
</style>
